package org.lex.ftplist.ui;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ActionMap;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;

import org.jdesktop.application.Application;
import org.jdesktop.application.FrameView;

public class FLMainView extends FrameView {
	private MainContentPane contentPane = null;
	private JMenuBar menuBar = null;

	public FLMainView(Application app) {
		super(app);
		this.initialize();
	}

	JDesktopPane getDesktopPane() {
		return this.createContentPane().getDesktopPane();
	}

	private void initialize() {
		 this.setMenuBar(this.createMenuBar());
		 this.setComponent(this.createContentPane());
		JFrame frame = this.getFrame();
		frame.setName("mainFrame");
		frame.setSize(400, 300);
		frame.setContentPane(this.createContentPane());
		frame.setJMenuBar(this.createMenuBar());
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				getApplication().exit(e);
			}
		});
	}

	private MainContentPane createContentPane() {
		if (null == contentPane) {
			this.contentPane = new MainContentPane();
		}
		return contentPane;
	}

	private JMenuBar createMenuBar() {
		if (null == menuBar) {
			ActionMap am = getContext().getActionMap(getApplication());

			JMenu fileMenu = new JMenu("File");
			fileMenu.add(am.get(Actions.newFtpList));
			fileMenu.addSeparator();
			fileMenu.add(am.get(Actions.exit));

			JMenu toolMenu = new JMenu("Tool");
			toolMenu.add(am.get(Actions.viewCache));

			JMenu helpMenu = new JMenu("Help");
			helpMenu.add(am.get(Actions.about));
			menuBar = new JMenuBar();
			menuBar.add(fileMenu);
			menuBar.add(toolMenu);
			menuBar.add(helpMenu);
		}
		return menuBar;
	}
}
